package com.kye.express.business.help.list;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.kye.express.App;
import com.kye.express.R;
import com.kye.express.business.base.BaseToolbarActivity;
import com.kye.express.business.help.search.HelpSearchActivity;
import com.kye.express.databinding.ActivityHelpBinding;

/**
 * 帮助 Activity
 * <p>
 * Created by SkyYu on 2018/01/9
 */
public class HelpActivity extends BaseToolbarActivity implements HelpContract.HelpView {

    private ActivityHelpBinding mBinding;
    private HelpContract.HelpPresenter mPresenter;

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, HelpActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) mPresenter.unbind();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_help_search, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_help_search:
                startHelpSearchActivity();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        initBinding();
        initPresenter();
        initTitle();
    }

    private void initData() {
        initViewPaper();
    }

    private void initBinding() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_help);
    }

    private void initPresenter() {
        mPresenter = new HelpContract.HelpPresenter();
        mPresenter.bind(this);
    }

    private void initTitle() {
        setUpToolbar(mBinding.toolbar);
        setTitle(R.string.help_help);
    }

    private void initViewPaper() {
        String[] titles = App.getContext().getResources().getStringArray(R.array.help_list_title);
        HelpFragmentPageAdapter mPageAdapter = new HelpFragmentPageAdapter(getSupportFragmentManager(), titles);
        mBinding.viewPagerContent.setAdapter(mPageAdapter);
        mBinding.viewPagerContent.setOffscreenPageLimit(titles.length);
        mBinding.tabLayout.setViewPager(mBinding.viewPagerContent, titles);
        mBinding.viewPagerContent.setCurrentItem(0);
    }

    @Override
    public void startHelpSearchActivity() {
        HelpSearchActivity.startActivity(this);
    }
}